from fastmcp import Client
import asyncio
from fastmcp.client.logging import LogMessage

class McpTOOL:
    def __init__(self, http_url: str) -> None:
        self.HTTP_URL = http_url

    async def call_mcp_tools(self):
        async def log_handler(message: LogMessage):
            print(f"[Server Log - {message.level.upper()}] {message.logger or 'default'}: {message.data}")

        async def my_progress_handler(progress: float, total: float | None, message: str | None) -> None:
            print(f"Progress: {progress} / {total} ({message})")

        async with Client(self.HTTP_URL, log_handler=log_handler,
                          progress_handler=my_progress_handler) as client:
            await client.ping()
            print("✅ MCP Server 可达")
            print(f"✅ 客户端已连接: {client.is_connected()}")
            tools = await client.list_tools()
            print(f"🔧 可用工具列表: {tools}")

    async def startmcp(self):
        asyncio.run(self.call_mcp_tools())
